﻿using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;

namespace Yz.Cache
{
    public class CacheAppProvider
    {
        public IMemoryCache _memoryCache { get; set; }

        public void Delete(string key)
        {
            _memoryCache.Remove(key);
        }

        public async Task DeleteAsync(string key)
        {
            await Task.Run(() => _memoryCache.Remove(key));
        }
        public bool Contains(string key, out object obj)
        {
            return _memoryCache.TryGetValue(key, out obj);
        }
        public void Set(string key, object value, TimeSpan timeSpan)
        {
            _memoryCache.Set(key, value, timeSpan);
        }
        public void Set<T>(string key, T value, TimeSpan timeSpan)
        {
            _memoryCache.Set<T>(key, value, timeSpan);
        }
        public async Task SetAsync(string key, object value, TimeSpan timeSpan)
        {
            await Task.Run(() => _memoryCache.Set(key, value, timeSpan));
        }
        public async Task SetAsync<T>(string key, T value, TimeSpan timeSpan)
        {
            await Task.Run(() => _memoryCache.Set<T>(key, value, timeSpan));
        }
        public object Get(string key)
        {
            return _memoryCache.Get(key);
        }
        public T Get<T>(string key)
        {
            return _memoryCache.Get<T>(key);
        }
        public async Task<object> GetAsync(string key)
        {
            return await Task.Run(() => _memoryCache.Get(key));
        }
        public async Task<T> GetAsync<T>(string key)
        {
            return await Task.Run(() => _memoryCache.Get<T>(key));
        }
    }
}
